<?php

namespace App\Http\Controllers\Api\Home;

use App\Http\Controllers\Api\Controller;
use App\Http\Requests\Api\CaptchaRequest;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Cache;
//TODO   验证码
class CaptchasController extends Controller
{
    //

    public function store(CaptchaRequest $request,CaptchaBuilder $captchaBuilder){
        $key = 'captcha-' .str_random(15);
        $phone = $request->phone;

        $captcha = $captchaBuilder->build();

        $expiredAt = now()->addMinute(100);

        Cache::put($key,['phone'=>$phone,'code'=>$captcha->getPhrase()],$expiredAt);

        $result = [
            'captcha_key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
            'captcha_image' => $captcha->inline()
        ];

        return $this->response->array($result)->setStatusCode(201);
    }
}
